<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div>
        <h1>The Ajax 02 Page</h1>
        <button onclick="doAjaxGet()">Do Ajax Get</button>
        <button onclick="doAjaxPost()">Do Ajax Post</button>
        <button onclick="doAjaxPut()">Do Ajax Put</button>
        <button onclick="doAjaxDelete()">Do Ajax Delete</button>
    </div>
    <div id="result"></div>
    <script>
        //Get 请求方法应用,主要从服务端获取数据
        function doAjaxGet(){
            let xhr=new XMLHttpRequest();
            xhr.onreadystatechange=function (){
                if(xhr.readyState==4&&xhr.status==200){
                    document.getElementById("result").innerHTML=xhr.responseText;
                }
            }
            xhr.open("GET","http://localhost/doAjaxGet",true);
            xhr.send(null);//get请求send方法内部不穿具体数据,可为null,也可不写
        }
        //Post请求
        function doAjaxPost(){
            let xhr=new XMLHttpRequest();
            xhr.onreadystatechange=function (){
                if(xhr.readyState==4&&xhr.status==200){
                    document.getElementById("result").innerHTML=xhr.responseText;
                }
            }
            xhr.open("POST","http://localhost/doAjaxPost",true);
            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
            xhr.send("id=200&name=小米&remark=好");//get请求send方法内部不穿具体数据,可为null,也可不写
        }
        //Delete请求
        function doAjaxDelete(){
            debugger
            let xhr=new XMLHttpRequest();
            xhr.onreadystatechange=function (){
                if(xhr.readyState==4&&xhr.status==200){
                    document.getElementById("result").innerHTML=xhr.responseText;
                }
            }
            xhr.open("delete","http://localhost/doAjaxDelete/200",true);
            xhr.send();
        }
        //update请求
        function doAjaxPut(){
            let xhr=new XMLHttpRequest();
            xhr.onreadystatechange=function (){
                if(xhr.readyState==4&&xhr.status==200){
                    document.getElementById("result").innerHTML=xhr.responseText;
                }
            }
            xhr.open("PUT","http://localhost/doAjaxUpdate",true);
            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
            xhr.send("id=200&name=苹果&remark=垃圾");//put请求数据要写到send方法内部
        }
    </script>
</body>
</html>